package com.changgou.config.service.impl;

import com.changgou.config.dao.FreightTemplateMapper;
import com.changgou.config.pojo.FreightTemplate;
import com.changgou.config.service.FreightTemplateService;
import com.changgou.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FreightTemplateServiceImpl implements FreightTemplateService {
    @Autowired
    private FreightTemplateMapper freightTemplateMapper;

    @Override
    public void delete(int id) {
        freightTemplateMapper.deleteByPrimaryKey(id);
    }

    /**
     * 添加运费模板
     * @param freightTemplate
     */
    @Override
    public void add(FreightTemplate freightTemplate) {
        freightTemplateMapper.insertSelective(freightTemplate);
    }

    /**
     * 查询模板列表
     * fxq
     * @return
     * @param page
     * @param size
     */
    @Override
    public PageResult<FreightTemplate> findAll(int page, int size) {
        //设置分页
        PageHelper.startPage(page,size);
        Page<FreightTemplate> list = (Page<FreightTemplate>) freightTemplateMapper.selectAll();
        //设置地区数量
        //遍历集合
        for (FreightTemplate freightTemplate : list) {
            //获取地区字符串
            String regions = freightTemplate.getRegions();
            //分割
            String[] split = regions.split(",");
            //设置地区数量
            freightTemplate.setRegionNum(split.length);
        }
        //创建PageResult
        PageResult pageInfo = new PageResult(list.getTotal(),list.getResult());

        return pageInfo;
    }

    /**
     * 模板下拉框查询
     * @return
     */
    @Override
    public List<FreightTemplate> findList() {
        List<FreightTemplate> freightTemplates = freightTemplateMapper.selectAll();
        return freightTemplates;
    }
}
